package com.zh.device.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zh.common.utils.RedisUtil;
import com.zh.device.dao.RedisDeviceDao;
import com.zh.device.domain.BehaviorDO;
import com.zh.device.po.DevcieExample;
import com.zh.device.po.DeviceRela;
import com.zh.device.service.BehaviorService;
import com.zh.device.service.RedisDeviceService;
import com.zh.device.util.DeviceUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class RedisDeviceServiceImpl implements RedisDeviceService {


    @Autowired
    private RedisDeviceDao redisDeviceDao;

    @Autowired
    private BehaviorService behaviorService;

    @Override
    public void initDeviceData(Map<String,String> map) {

        List<DevcieExample> devcieExamples = redisDeviceDao.findAllDevcieExample(map);

        for (DevcieExample devcieExample : devcieExamples) {
            devcieExample.setBehaviorDO(this.getDevRelaToDo(devcieExample.getDeviceRelas(),devcieExample.getDeviceSn()));
            if (RedisUtil.hasKey(devcieExample.getDeviceSn())) {
                DevcieExample device = (DevcieExample) RedisUtil.get(devcieExample.getDeviceSn());
                //保留上次设备的传输记录
                devcieExample.setJsonArray(device.getJsonArray());
            }
            RedisUtil.del(devcieExample.getDeviceSn());
            RedisUtil.set(devcieExample.getDeviceSn(),devcieExample);
        }

        System.out.println("OJBK");
    }

    @Override
    public Integer getDeviceType(String deviceSn) {
        if (RedisUtil.hasKey(deviceSn)) {
            DevcieExample device = (DevcieExample) RedisUtil.get(deviceSn);
            return device.getDeviceType();
        }
        return null;
    }

    /**
     * 根据硬件编号获取硬件绑定的对于关系
     * 0网关 1老人id 2家庭id 3场景id
     *
     * @param deviceSn
     * @return
     */
    @Override
    public BehaviorDO getDevRelaToDo(List<DeviceRela> deviceRelas,String deviceSn) {

        BehaviorDO behaviorDO = new BehaviorDO();
        for (DeviceRela deviceRela : deviceRelas) {
            if (0 == deviceRela.getBindingType()) {
                behaviorDO.setGatewayId(deviceRela.getDeviceBindingId());
            }
            if (1 == deviceRela.getBindingType()) {
                behaviorDO.setFamilyId(deviceRela.getDeviceBindingId());
                behaviorDO.setFamilyUserName(deviceRela.getName());
            }
            if (2 == deviceRela.getBindingType()) {
                behaviorDO.setFamilyId(deviceRela.getDeviceBindingId());
                behaviorDO.setFamilyName(deviceRela.getName());
            }
            if (3 == deviceRela.getBindingType()) {
                behaviorDO.setSceneId(deviceRela.getDeviceBindingId());
                behaviorDO.setSceneName(deviceRela.getName());
            }
        }
        behaviorDO.setEquipmentId(deviceSn);
        return behaviorDO;

    }


    @Override
    public void prossonDevice(Integer behaviorType, JSONObject jsonParam, String deviceSn) throws Exception{
        DevcieExample device = (DevcieExample) RedisUtil.get(deviceSn);
        device.getBehaviorDO().setBehaviorType(behaviorType);
        device.getBehaviorDO().setRemark(jsonParam.getString("message"));
        device.getBehaviorDO().setStartTime(DeviceUtil.formatCreateByDate(jsonParam.getString("timeCreate")));
        device.getBehaviorDO().setAnomalies(jsonParam.getInteger("anomalies"));
        behaviorService.save(device.getBehaviorDO());
        device.setArrSize(1);
        device.setJsonArray(null);
    }


 /*   @Override
    public void prossonDevice(Integer behaviorType, JSONObject jsonParam, String deviceSn) throws Exception{
            DevcieExample device = (DevcieExample) RedisUtil.get(deviceSn);
            if(device.getArrSize()==5){
                device.getBehaviorDO().setBehaviorType(behaviorType);
                device.getBehaviorDO().setRemark(jsonParam.getString("message"));
                device.getBehaviorDO().setStartTime(DeviceUtil.formatCreateByDate(jsonParam.getString("timeCreate")));
                behaviorService.save(device.getBehaviorDO());
                device.setArrSize(1);
                device.setJsonArray(null);
            }else{
                JSONArray jsonArray;
                if(null== device.getJsonArray()){
                    jsonArray=new JSONArray();
                }else{
                    jsonArray= device.getJsonArray();
                }
                jsonArray.add(jsonParam);
                device.setArrSize(device.getArrSize()+1);
            }
            RedisUtil.set(deviceSn,device);
    }*/
}
